import api from './index'

// 使用 window.electronAPI 替代直接的 Node.js API 调用
const { electronAPI } = window

// 房间相关的 API
const roomAPI = {
  getRoomStatus: () => api.get('/api/rooms/status'),
  getRooms: () => api.get('/api/rooms'),
  createRoom: (data) => api.post('/api/rooms', data),
  updateRoom: (id, data) => api.put(`/api/rooms/${id}`, data),
  deleteRoom: (id) => api.delete(`/api/rooms/${id}`)
}

// 零食相关的 API
const snackAPI = {
  getSnacks: () => api.get('/api/snacks'),
  createSnack: (data) => api.post('/api/snacks', data),
  updateSnack: (id, data) => api.put(`/api/snacks/${id}`, data),
  deleteSnack: (id) => api.delete(`/api/snacks/${id}`),
  adjustStock: (id, adjustment) => api.put(`/api/snacks/stock/${id}`, { adjustment }),
  getCategories: () => api.get('/api/categories'),
  createCategory: (data) => api.post('/api/categories', data),
  updateCategory: (id, data) => api.put(`/api/categories/${id}`, data),
  deleteCategory: (id) => api.delete(`/api/categories/${id}`)
}

// 订单相关的 API
const orderAPI = {
  getOrders: () => api.get('/api/orders'),
  createOrder: (data) => api.post('/api/orders/', data),
  getOrderHistory: () => api.get('/api/orders/history'),
  getCurrentOrder: (orderId) => api.get(`/api/orders/${orderId}`),
  preCheckout: (orderId) => api.get(`/api/orders/${orderId}/pre-checkout`),
  checkout: (orderId, data) => api.post(`/api/orders/${orderId}/checkout`, data),
  addOrderItem: (orderId, data) => api.post(`/api/orders/${orderId}/items`, data),
  deleteOrderItem: (orderId, itemId) => api.delete(`/api/orders/${orderId}/items/${itemId}`)
}

// 用户相关的 API
const userAPI = {
  login: (username, password) => {
    const formData = new FormData()
    formData.append('username', username)
    formData.append('password', password)
    return api.post('/api/token', formData)
  },
  getProfile: () => api.get('/api/users/me'),
  changePassword: (data) => api.post('/api/users/change-password', {
    old_password: data.old_password,
    new_password: data.new_password
  })
}

// 报表相关的 API
const reportAPI = {
  // 获取营业报表数据
  getDailyReport: () => api.get('/api/reports/daily'),
  
  // 获取营业统计数据
  getStatistics: () => api.get('/api/reports/statistics'),

  // 获取指定日期范围的报表
  getReportByDateRange: (startDate, endDate) => api.get(`/api/reports/range?start_date=${startDate}&end_date=${endDate}`)
}

// 打印相关的 API
const printAPI = {
  // 打印小票
  printReceipt: (data) => api.post('/api/print/receipt', {
    order_id: data.order_id,
    room_name: data.room_name,
    start_time: data.start_time,
    end_time: data.end_time,
    items: data.items,
    base_amount: data.base_amount,
    snack_amount: data.snack_amount,
    adjustment: data.adjustment,
    remark: data.remark,
    total_amount: data.total_amount
  }),

  // 打印标签
  printLabel: (data) => api.post('/api/print/label', {
    name: data.name,
    price: data.price
  }),

  // 获取打印机配置
  getPrinterConfigs: () => api.get('/api/printer/config'),
  
  // 更新打印机配置
  updatePrinterConfig: (type, config) => api.put(`/api/printer/config/${type}`, config),
  
  // 获取系统打印机列表
  getSystemPrinters: () => api.get('/api/printer/system-printers')
}

// 统一导出所有 API
export {
  userAPI,
  roomAPI,
  snackAPI,
  orderAPI,
  reportAPI,
  printAPI
}